
#ifndef __RT_CONN_BASE_H__
#define  __RT_CONN_BASE_H__

#include "RtNetworkBase.h"
#include "RtTimerWrapper.h"

class CRtConnBase 
	: public IRtTransport 
	, public IRtTransportSink 
	, public IRtAcceptorConnectorSink 
	, public CRtTimerWrapperSink
	, public CRtReferenceControlSingleThreadTimerDelete
{
public:
	CRtConnBase()
		: m_sink(NULL)
		, m_status(RT_CONN_STATUS_INIT)
	{
	}
	virtual ~CRtConnBase()
	{
		Release();
	}

public:
	virtual RtResult OpenWithSink(IRtTransportSink* inSink) 
	{
		m_sink = inSink; 
		return RT_OK;
	}
	virtual IRtTransportSink* GetSink() { return m_sink; }

	virtual DWORD AddReference()
	{
		return CRtReferenceControlSingleThreadTimerDelete::AddReference();
	}

	virtual DWORD ReleaseReference()
	{
		return CRtReferenceControlSingleThreadTimerDelete::ReleaseReference();
	}

	IRtTransport* GetLowerTransport() const { return m_lowerTransport.ParaIn(); }

	void SetStatus(WORD inStatus) { m_status = inStatus; }
	WORD GetStatus() const { return m_status; }

protected:
	void Release() 
	{
		m_sink = NULL;
		if (m_lowerTransport)
		{
			m_lowerTransport->Disconnect(RT_OK);
			m_lowerTransport = NULL;
		}
		m_timer.Cancel();
	}

protected:
	IRtTransportSink* m_sink;
	CRtAutoPtr<IRtTransport> m_lowerTransport;
	WORD m_status;
	CRtTimerWrapper m_timer;
};

#endif //!__RT_CONN_BASE_H__

